package com.xiaomengkuan.user.controller;

import com.xiaomengkuan.common.exception.CasualException;
import com.xiaomengkuan.common.util.DetailSelector;
import com.xiaomengkuan.common.util.JwtUtil;
import com.xiaomengkuan.user.entity.vo.UserResponseVo;
import com.xiaomengkuan.user.remote.SmsFeignService;
import com.xiaomengkuan.common.util.R;
import com.xiaomengkuan.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author Faraj Mujey
 * -- Creation Time: 2021-01-12 3:12 下午, File Encoding: utf8 --
 */
@RestController
@Api(tags = "用户登陆")
@RequestMapping("user/login")
public class LoginController {

    @Resource
    private SmsFeignService smsFeignService;

    @Resource
    private UserService userService;

    /**
     * 使用密码登陆API
     *
     * @param requestBody 用户名、密码
     * @return R
     */
    @ApiOperation("使用密码登陆")
    @PostMapping("passwordLogin")
    public R passwordLogin(@RequestBody Map<String, Object> requestBody) {
        UserResponseVo userObj = userService.passwordLogin(requestBody);
        if (!StringUtils.isEmpty(userObj)) {
            String token = getToken(userObj);
            // 返回数据
            return R.ok().detail(DetailSelector.LOGIN_SUCCESS)
                    .put("user", userObj)
                    .put("uuid", UUID.randomUUID().toString().replaceAll("-", ""))
                    .put("name", userObj.getUsername())
                    .put("token", token);
        } else
            throw new CasualException(5000, "登陆出现异常");
    }

    /**
     * 调用第三方服务发送验证短信
     *
     * @param phone 手机号
     * @return R
     */
    @ApiOperation("调用第三方服务发送验证短信")
    @GetMapping("getCode/{phone}")
    public R getCode(@PathVariable("phone") String phone) {
        return smsFeignService.sendMessage(phone);
    }

    /**
     * 手机号登陆API
     *
     * @param requestBody 手机号、验证码
     * @return UserResponseVo
     */
    @ApiOperation("手机号登陆")
    @PostMapping("phoneLogin")
    public R phoneLogin(@RequestBody Map<String, Object> requestBody) {
        UserResponseVo userObj = userService.phoneLogin(requestBody);
        if (!StringUtils.isEmpty(userObj)) {
            String token = getToken(userObj);
            return R.ok().detail(DetailSelector.LOGIN_SUCCESS).put("user", userObj)
                    .put("token", token);
        } else
            throw new CasualException(5000, "登陆出现异常");
    }

    @ApiOperation(value = "查询手机号是否已经存在")
    @GetMapping("checkPhone/{phone}")
    public R checkPhone(@PathVariable String phone) {
        return userService.checkPhone(phone);
    }

    /**
     * 修改密码
     *
     * @param requestBody 用户、密码、新密码
     * @return R
     */
    @ApiOperation(value = "修改密码")
    @PostMapping("modifyPassword")
    public R modifyPwd(@RequestBody Map<String, Object> requestBody) {
        boolean flag = userService.modifyPwd(requestBody);
        return flag ? R.ok().detail(DetailSelector.UPDATE_SUCCESS) :
                R.err().detail(DetailSelector.UPDATE_ERROR);
    }


    // 抽取的公用方法 获取token字符串
    private String getToken(UserResponseVo userObj) {
        String userId = userObj.getId();
        if (!StringUtils.isEmpty(userId)) {
            Map<String, String> jwtMap = new HashMap<>();
            jwtMap.put("id", userId);
            return JwtUtil.getToken(jwtMap);
        } else {
            throw new CasualException(5000, "用户不存在异常");
        }
    }
}
